#pragma rtGlobals=1		// Use modern global access method.

Function tougaard(bEnergyW, spectraW)
Wave bEnergyW, spectraW
String Benergy = NameofWave(bEnergyW), spectra = NameofWave(spectraW)
Wave BE = $Benergy, jE = $spectra
String nameHolder = spectra + "_tg"
String nameHolder2 = spectra + "_bg"
String nameHolder3 = spectra + "_bgV"
String nameHolder4 = "KE_" + spectra
Duplicate/O spectraW,$nameHolder,$nameHolder2,$nameHolder3,$nameHolder4,intBackground
Make/O/N=(10) preEdge

Variable i = 0

Do
	preEdge[i] = jE[numpnts(jE)-1-i]
	i+=1
while(i < 10)

Variable avg = sum(preEdge)/10

i=0

Do 
	intBackground[i] = avg
	i+=1
while(i<numpnts(jE))

Wave fE = $nameHolder
Wave bgT = $nameHolder2, bg = $nameHolder3, KE = $nameHolder4

Variable e = 0, B =73, C =260, D = 62, hv = 4000, ep = 0
//Variable e = 0, B =6500, C =900, D = 13300, hv = 1486.6, ep = 0
KE = (hv - BE) 
Wavestats/Q jE
//jE = jE - V_min
Do
	ep = e
	Do
		bg[ep] = (B*(KE[ep] - KE[e])/((C - (KE[ep] - KE[e])^(2))^2 + D*(KE[ep] - KE[e])^(2)))*(jE[ep]-intBackground[ep])
		ep+=1
	while(ep < numpnts(jE))
	Integrate/METH=1 bg/D=bg_INT/X=KE
	fE[e] = (jE[e]) - ((bg_INT[numpnts(jE)-1]-bg_INT[e]))
	bgT[e] = ((bg_INT[numpnts(jE)-1]-bg_INT[e]))
	e+=1
while(e < numpnts(jE))

KillWaves/Z bg,bg_INT
end

Function shirley(Benergy, spectra)
String Benergy, spectra
Wave BE = $Benergy, jE = $spectra
String nameHolder = spectra + "_shirley"
String nameHolder2 = spectra + "_sbackground"
String nameHolder3 = spectra + "_bgV"
String nameHolder4 = "KE_" + spectra
Make/O/N=(numpnts(jE)) $nameHolder
Make/O/N=(numpnts(jE)) $nameHolder2
Make/O/N=(numpnts(jE)) $nameHolder3
Make/O/N=(numpnts(jE)) $nameHolder4
Make/O/N=(10) preEdge
Make/O/N=(numpnts(jE)) shirleyIt

Wave fE = $nameHolder
Wave bg = $nameHolder3, bgS = $nameHolder2,  KE = $nameHolder4

Variable i = 0, j = 0

Do
	preEdge[i] = jE[numpnts(jE)-1-i]
	i+=1
while(i < 10)

Variable avg = sum(preEdge)/10

i=0

Do 
	shirleyIt[i] = avg
	i+=1
while(i<numpnts(jE))

Do
	Variable e = 0, ep
	Do
	ep = e
		
		Do
		bg[ep] = (jE[ep] - shirleyIt[ep])
		ep+=1
		while(ep < numpnts(jE))
	Integrate/METH=1 bg/D=bg_INT/X=KE
	bgS[e] = bg_INT[numpnts(jE)-1]-bg_INT[e]
	e+=1
	while(e<numpnts(jE))
	
	Variable l = 0
	Wavestats/Q bgS
	bgS = bgS
	Do
		shirleyIt[l] =  bgS[l]
		l+=1
	while(l < numpnts(jE))
	
	j+=1
while(j < 7)

fE = jE - bgS

KillWaves/Z bg, shirleyIt, preEdge,bg_INT
end

Function tougaardList(inputWaveList)
String inputWaveList

Variable p1
For(p1 = 0; p1 < itemsinlist(inputWaveList); p1 += 1)
	Wave tempWave = $stringFromList(p1,inputWaveList)
	Wave tempBEWave = $("BE_" + stringFromList(p1,inputWaveList))
	tougaard(tempBEWave,tempWave)
endfor
end

Function bindingEnergyWaveFromScale(inputWave)
	Wave inputWave
	Make/O/N=(numpnts(inputWave)) $("BE_" + nameOfWave(inputWave))
	Wave outputWave = $("BE_" + nameOfWave(inputWave))
	outputWave[] = dimdelta(inputWave,0)*x+dimOffset(inputWave,0)
end